<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台列表控制器
class ListController extends Controller{
	private $model;
	private $cateModel;
	private $goodsAttrModel;
	private $brandModel;
	public function __init(){
		//实例化商品表模型
		$this->model = new \Common\Model\shopGoods;
		//实例化分类表
		$this->cateModel = new \Common\Model\shopCate;
		//实例化商品属性表
		$this->goodsAttrModel = new \Common\Model\shopGoodsAttr;
		//实例化品牌表
		$this->brandModel = new \Common\Model\shopBrand;
	}
	
	//默认
	public function index(){
		//接收cid
		$cid = Q('get.cid',0,'intval');
		//品牌表bid
		$bid = Q('get.bid',0,'intval');
		//获得顶级分类
		$CateData = $this->cateModel->where("pid=0 AND cid=7")->get();
		View::with('CateData1',$CateData);
		//1.根据当前的cid获得对应商品的gid(自己和子集的)
		$gids = $this->getNoMy($cid);
		//判断如果分类下没有商品
		if(!$gids)View::error('分类下暂无商品');
		//2.根据gid找到商品属性表(获得商品的属性和规格)
		$goodsAttr = $this->gidGetAttr($gids);
		View::with('goodsAttr',$goodsAttr);
		//3.处理筛选参数param 0_0_..
		//0_0_0的长度不能写死,是看商品属性有多少个
		$num = count($goodsAttr);
		$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
		View::with('param',$param);
		//4.通过param参数筛选商品的gid
		$resultGids = $this->filter($param,$gids);
		//5.通过商品的gid获得商品，
		$data = $this->model->where("gid in(" . implode(',', $resultGids) . ")")->get();
		//----品牌下面的商品----
		if($bid){
			$data = $this->model->where("bid={$bid}")->get();
		}
		View::with('data',$data);
		//品牌表
		$brandData = $this->brandModel->get();
		View::with('brandData',$brandData);
		//默认模板
	    View::make();
	}
	
	//4.通过param参数筛选商品的gid
	private function filter($param,$cidGids){
		$gids = array();
		foreach ($param as $v) {
			if($v){
				//根据商品属性id找出商品名称
				$gavalue = $this->goodsAttrModel->where("gaid={$v}")->pluck('gavalue');
				//因为商品名称有重复的,所以要用数组的形式(找出商品id)
				$gids[] = $this->goodsAttrModel->where("gavalue='{$gavalue}'")->lists('goods_gid');
			}
		}
		//如果点了“不限”之外的属性
		if($gids){
			$arr = $gids[0];
			foreach ($gids as $v) {
				$arr = array_intersect($arr, $v);
			}
			return array_intersect($arr, $cidGids);
		}else{
			return $cidGids;
		}
	}
	
	//2.根据gid找到商品属性表
	private function gidGetAttr($gids){
		//因为gids是数组,所以要拆分数组(因为多个值所以用in)
		$data = $this->goodsAttrModel->where("goods_gid in(" . implode(',', $gids) . ")")->groupBy('gavalue')->get();
		//1.把相同类型属性id的归结在一起
		$arr = array();
		foreach ($data as $v) {
			$arr[$v['type_taid']][] = $v;
		}
		//2.组合数组
		$arrs = array();
		foreach ($arr as $type_taid => $v) {
			$arrs[] = array(
				'name' => Db::table('shop_type_arrt')->where("taid={$type_taid}")->pluck('taname'),
				'value' => $v
			);
		}
		return $arrs;
	}
	
	//1.获得自己和子集分类的商品id
	private function getNoMy($cid){
		//通过当前的cid获得对应的所有的子集cid
		$cids = $this->getSon($this->cateModel->get(),$cid);
		//把自己压入数组
		$cids[] = $cid;
		//通过自己和子集cid查找对应的商品的gid
		$gids = $this->model->where("cid in(" . implode(',', $cids) . ")")->lists('gid');
		return $gids;
	}
	//获得子集
	private function getSon($data,$cid){
		//创建空数组
		$temp = array();
		//循环分类表
		foreach ($data as $v) {
			//父级的cid等于子集的pid
			if($v['pid']==$cid){
				//把获得子集的cid压入新数组
				$temp[] = $v['cid'];
				//递归(合并数组并找出所有子集)
				$temp = array_merge($temp,$this->getSon($data,$v['cid']));
			}
		}
		return $temp;
	}
	
	
	
	
	
	
	
	
	
	
}